package com.bolt.convention.data;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2019/4/19.
 */
public class DefaultPageInfo<T> implements PageInfo<T> {

    public static final int DEFAULT_TOTAL_PAGES = 1;
    public static final int DEFAULT_TOTAL_COUNT = 0;
    /**
     * 第几页
     */
    private int pageNumber;
    /**
     * 每页多少条记录数
     */
    private int pageSize;
    /**
     * 总页
     */
    private int totalPages;
    /**
     * 总记录数
     */
    private long totalCount;

    /**
     * 结果集合
     */
    private List<T> content;

    public DefaultPageInfo() {
        this.content = Collections.emptyList();
        this.totalPages = DEFAULT_TOTAL_PAGES;
        this.totalCount = DEFAULT_TOTAL_COUNT;
        this.pageNumber = PageQuery.DEFAULT_PAGE_NUMBER;
        this.pageSize = PageQuery.DEFAULT_PAGE_SIZE;
    }

    public DefaultPageInfo(int pageNumber, int pageSize, List<T> results, long totalCount) {
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.content = Objects.nonNull(results) ? results : Collections.emptyList();
        this.totalPages = pageSize == 0 || totalCount == 0 ?
                1 : (int) Math.ceil((double) totalCount / (double) this.pageSize);
    }

    public DefaultPageInfo(Pagination pageable, List<T> results, long totalCount) {
        this(pageable.getPageNumber(), pageable.getPageSize(), results, totalCount);
    }

    @Override
    public int getPageNumber() {
        return pageNumber;
    }

    @Override
    public int getPageSize() {
        return pageSize;
    }

    @Override
    public long getTotalCount() {
        return totalCount;
    }

    @Override
    public List<T> getContent() {
        return content;
    }

    @Override
    public int getTotalPages() {
        return totalPages;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    public void setResults(List<T> results) {
        this.content = Objects.nonNull(results) ? results : Collections.emptyList();
    }

    @Override
    public <S> PageInfo<S> map(Function<? super T, ? extends S> converter) {
        return new DefaultPageInfo<>(pageNumber, pageSize, Objects.nonNull(content) ?
                content.stream().map(converter).collect(Collectors.toList()) :
                Collections.emptyList(), totalCount);
    }

    @Override
    public String toString() {
        return "DefaultPageInfo{" +
                "pageNumber=" + pageNumber +
                ", pageSize=" + pageSize +
                ", totalPages=" + totalPages +
                ", totalCount=" + totalCount +
                ", content=" + content +
                '}';
    }
}

 